/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.agent.service;

import cn.ac.iie.agent.tools.XmlTools;
import java.util.Map;
import org.apache.log4j.Logger;

/**
 *
 * @author dell
 */
public class FtpDownloadService {

    private static Logger logger = Logger.getLogger(FtpDownloadService.class);

    public static Map<String, String> execute(String xml, Map<String, String> rsMap, String subjobid) {
        //ftp信息
        String ip = XmlTools.getValueFromStrDGText(xml, "ip");
        String port = XmlTools.getValueFromStrDGText(xml, "port");
        String username = XmlTools.getValueFromStrDGText(xml, "username");
        String password = XmlTools.getValueFromStrDGText(xml, "password");
        String filepath = XmlTools.getValueFromStrDGText(xml, "filepath");
        logger.info(subjobid + " the receive ftp's message is[ip: " + ip + ",port: " + port + ",username: " + username + ",password: " + password + ",filepath: " + filepath + "]");
        if (ip.isEmpty() || port.isEmpty() || username.isEmpty() || password.isEmpty() || filepath.isEmpty()) {
            logger.info(subjobid + " one of the ftp parameters is empty! Please chaeck!");
            rsMap.put("status", "fail");
            rsMap.put("desc", "One of the ftp parameters is empty! Please chaeck!");
            rsMap.put("total", "0");
            rsMap.put("complete", "0");
            return rsMap;
        }

        //分配所得的json文件
        String jsonFiles = XmlTools.getElValueFromStr(xml, "jsonfilesname");
        logger.info(subjobid + " the jsonfilesname is: " + jsonFiles);
        if (jsonFiles.isEmpty()) {
            logger.info(subjobid + " No files can be downloaded!");
            rsMap.put("status", "fail");
            rsMap.put("desc", "No files can be downloaded!");
            rsMap.put("total", "0");
            rsMap.put("complete", "0");
            return rsMap;
        }

        new FtpDownloadJsonFilesThread(subjobid, ip, port, username, password, filepath, jsonFiles, rsMap).execute();
        return rsMap;
    }
}
